package com.woniu108.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.car.param.*;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.car.CarException;
import com.woniu108.carService.commos.exception.car.CarExceptionCode;
import com.woniu108.carService.commos.util.StaticData;
import org.springframework.stereotype.Service;
import com.woniu108.car.client.PersonClient;
import com.woniu108.car.mapper.CarBrandMapper;
import com.woniu108.car.mapper.CarHotMapper;
import com.woniu108.car.model.CarBrand;
import com.woniu108.car.model.CarHot;
import com.woniu108.car.service.CarHotService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 车辆品牌热度表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class CarHotServiceImpl extends ServiceImpl<CarHotMapper, CarHot> implements CarHotService {
    @Resource
    private CarHotMapper carHotMapper;
    @Resource
    private PersonClient personClient;
    @Resource
    private CarBrandMapper carBrandMapper;

    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo==null){
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
        if (adminInfo.getAdminRoleId() != 1) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }

    @Override
    public List<CarHot> findAllCarHot() {
        QueryWrapper<CarHot> wrapper = new QueryWrapper<>();
        wrapper.ne("hot_state", StaticData.CAR_HOT_STATE_DELETE)
                .orderByDesc("hot_num");
        return carHotMapper.selectList(wrapper);
    }

    @Override
    public List<CarHot> findAllNormalCarHot() {
        QueryWrapper<CarHot> wrapper = new QueryWrapper<>();
        wrapper.eq("hot_state", StaticData.CAR_HOT_STATE_NORMAL)
                .orderByDesc("hot_num");
        return carHotMapper.selectList(wrapper);
    }

    @Override
    public Boolean insertCarHot(InsertCarHotParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        //查询品牌是否存在，和状态是否可用
        CarBrand carBrand = carBrandMapper.selectById(param.getBrandId());
        if (carBrand==null){
            throw new CarException(CarExceptionCode.CAR_BRAND_NOT_HAVE);
        }
        if (Objects.equals(carBrand.getBrandState(), StaticData.CAR_BRAND_STATE_BAN)){
            throw new CarException(CarExceptionCode.CAR_BRAND_STATE_BAN);
        }
        if ((Objects.equals(carBrand.getBrandState(), StaticData.CAR_BRAND_STATE_DELETE))){
            throw new CarException(CarExceptionCode.CAR_BRAND_STATE_DELETE);
        }
        //查询热度榜是否已经有该品牌
        CarHot hot = carHotMapper.selectById(param.getBrandId());
        if (hot!=null){
            if (Objects.equals(hot.getHotState(), StaticData.CAR_HOT_STATE_BAN)
                    || Objects.equals(hot.getHotState(), StaticData.CAR_HOT_STATE_NORMAL)){
                throw new CarException(CarExceptionCode.CAR_HOT_HAVE);
            }
            hot.setHotState(StaticData.CAR_HOT_STATE_NORMAL);
            carHotMapper.updateById(hot);
        }else {
            CarHot carHot=new CarHot();
            carHot.setBrandId(param.getBrandId());
            carHot.setBrandName(carBrand.getBrandName());
            carHot.setHotNum(0);
            carHot.setHotState(StaticData.CAR_HOT_STATE_NORMAL);
            carHotMapper.insert(carHot);
        }
        return true;
    }

    @Override
    public Boolean deleteCarHot(DeleteCarHotParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        //删除汽车热度榜
        for (Integer brandId : param.getBrandIds()) {
            CarHot hot = carHotMapper.selectById(brandId);
            if (hot==null){
                throw new CarException(CarExceptionCode.CAR_HOT_NOT_HAVE);
            }
            if (Objects.equals(hot.getHotState(), StaticData.CAR_STYLE_STATE_DELETE)){
                throw new CarException(CarExceptionCode.CAR_BRAND_STATE_DELETE);
            }
            CarHot carHot = new CarHot();
            carHot.setBrandId(brandId);
            carHot.setHotState(StaticData.CAR_HOT_STATE_DELETE);
            int update = carHotMapper.updateById(carHot);
            if (update<=0){
                throw new CarException(CarExceptionCode.CAR_HOT_NOT_HAVE);
            }
        }
        return true;
    }

    @Override
    public Boolean banCarHot(BanCarHotParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        //禁用汽车热度榜
        for (Integer brandId : param.getBrandIds()) {
            CarHot hot = carHotMapper.selectById(brandId);
            if (hot==null){
                throw new CarException(CarExceptionCode.CAR_HOT_NOT_HAVE);
            }
            if (Objects.equals(hot.getHotState(), StaticData.CAR_STYLE_STATE_DELETE)){
                throw new CarException(CarExceptionCode.CAR_BRAND_STATE_DELETE);
            }
            if (Objects.equals(hot.getHotState(), StaticData.CAR_HOT_STATE_BAN)){
                throw new CarException(CarExceptionCode.CAR_HOT_STATE_BAN);
            }
            CarHot carHot = new CarHot();
            carHot.setBrandId(brandId);
            carHot.setHotState(StaticData.CAR_HOT_STATE_BAN);
            int update = carHotMapper.updateById(carHot);
            if (update<=0){
                throw new CarException(CarExceptionCode.CAR_HOT_NOT_HAVE);
            }
        }
        return true;
    }

    @Override
    public Boolean unlockCarHot(UnlockCarHotParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        //禁用汽车热度榜
        for (Integer brandId : param.getBrandIds()) {
            CarHot hot = carHotMapper.selectById(brandId);
            if (hot==null){
                throw new CarException(CarExceptionCode.CAR_HOT_NOT_HAVE);
            }
            if (Objects.equals(hot.getHotState(), StaticData.CAR_STYLE_STATE_DELETE)){
                throw new CarException(CarExceptionCode.CAR_BRAND_STATE_DELETE);
            }
            if (Objects.equals(hot.getHotState(), StaticData.CAR_HOT_STATE_NORMAL)){
                throw new CarException(CarExceptionCode.CAR_HOT_STATE_NORMAL);
            }
            CarHot carHot = new CarHot();
            carHot.setBrandId(brandId);
            carHot.setHotState(StaticData.CAR_HOT_STATE_NORMAL);
            int update = carHotMapper.updateById(carHot);
            if (update<=0){
                throw new CarException(CarExceptionCode.CAR_HOT_NOT_HAVE);
            }
        }
        return true;
    }

    @Override
    public Boolean updateCarHot(UpdateCarHotParam param) {
        //判断管理员权限
        isAdmin(param.getAdminId());
        CarHot carHot = carHotMapper.selectById(param.getBrandId());
        if (carHot==null){
            throw new CarException(CarExceptionCode.CAR_HOT_NOT_HAVE);
        }
        if (Objects.equals(carHot.getHotState(), StaticData.CAR_STYLE_STATE_DELETE)){
            throw new CarException(CarExceptionCode.CAR_BRAND_STATE_DELETE);
        }
        carHot.setHotNum(param.getHotNum());
        carHotMapper.updateById(carHot);
        return true;
    }

}
